import cfg from "@/config"
const { storage, tokenStorageKey } = cfg;

/**
 * @description 获取accessToken
 * @returns {string|null}
 */
export const getAccessToken = (): string | null => {
  if (storage) {
    if (storage === "localStorage") {
      return localStorage.getItem(tokenStorageKey)
    } else if ('sessionStorage' === storage) {
      return sessionStorage.getItem(tokenStorageKey)
    } else {
      return localStorage.getItem(tokenStorageKey)
    }
  }
  else {
    return localStorage.getItem(tokenStorageKey)
  }
}

/**
 * @description 设置accessToken
 * @param {string} accessToken
 * @returns {void}
 */
export const setAccessToken = (accessToken: string): void => {
  if (storage) {
    if (storage === "localStorage") {
      localStorage.setItem(tokenStorageKey, accessToken)
    } else if ('sessionStorage' === storage) {
      sessionStorage.setItem(tokenStorageKey, accessToken)
    } else {
      localStorage.setItem(tokenStorageKey, accessToken)
    }
  }
  else {
    localStorage.setItem(tokenStorageKey, accessToken)
  }
}

/**
 * @description 设置accessToken
 * @returns {void}
 */
export const removeAccessToken = (): void => {
  if (storage) {
    if (storage === "localStorage") {
      localStorage.removeItem(tokenStorageKey)
    } else if ('sessionStorage' === storage) {
      sessionStorage.removeItem(tokenStorageKey)
    } else {
      localStorage.removeItem(tokenStorageKey)
    }
  }
  else {
    localStorage.removeItem(tokenStorageKey)
  }
}